function [PQ, status, scheduledTargets] = IA_ScheduleRun_SCP_Parallel(PQ, s1TargetList, s2TargetList)
%IA_ScheduleRun_SCP Simple Constraint Programming
% Detailed explanation goes here

global schedPerf;

scheduledTargets = [];
status = 0;
randomTargetList = randperm(PQ_Size(PQ));
ia_network = IA_Network_Empty();

for p = 1:PQ_Priorities(PQ)
  for target = 1:length(randomTargetList)
    [Task, status] = PQ_Fetch_Task(PQ, randomTargetList(target), PQ_Priorities(PQ)+1-p);
    if ~status
      continue;
    end
    target_id = Task.id;
    ia_interval = IA_Interval(sprintf('T%i', target_id), target_id);
    ia_nodes = IA_Network_Nodes(ia_network);
    consistent = 1;
    ia_net_temp = IA_Network_Add_Interval(ia_network, ia_interval);
    for node = 1:ia_nodes
      node_id = ia_network.nodes(node).duration;
      if node_id ~= target_id
        [t_row1, ~] = ind2sub(size(s1TargetList), find(s1TargetList == target_id));
        [n_row1, ~] = ind2sub(size(s1TargetList), find(s1TargetList == node_id));
        
        if n_row1 < t_row1
          relation = IA_Relation_Less();
        elseif n_row1 == t_row1
          relation = IA_Relation_Dummy();
        else
          relation = IA_Relation_Greater();
        end
        
        [t_row2, ~] = ind2sub(size(s2TargetList), find(s2TargetList == target_id));
        [n_row2, ~] = ind2sub(size(s2TargetList), find(s2TargetList == node_id));
        
        if n_row2 < t_row2
          relation = IA_Relation_Intersect(relation, IA_Relation_Less());
        elseif n_row2 == t_row2
          relation = IA_Relation_Intersect(relation, IA_Relation_Dummy());
        else
          relation = IA_Relation_Intersect(relation, IA_Relation_Greater());
        end
        
        if IA_Relation_Is_Null(relation)
          consistent = 0;
          break;
        end
        
        [ia_net_temp, consistent, iterations] = IA_Network_Path_Consistency_Parallel(ia_net_temp, node, ia_nodes+1, relation);
        schedPerf.iterations = [schedPerf.iterations iterations];
        if ~consistent
          break;
        end
      end
    end
    if consistent
      ia_network = ia_net_temp;
      scheduledTargets = [scheduledTargets target_id]; %#ok<AGROW>
    end
  end
end

% Sort the scheduled targets by consulting the IA
for node1 = 1:size(scheduledTargets, 2)-1
  for node2 = node1+1:size(scheduledTargets, 2)
    [~, i] = IA_Network_Find_Interval(ia_network, sprintf('T%i', scheduledTargets(node1)));
    [~, j] = IA_Network_Find_Interval(ia_network, sprintf('T%i', scheduledTargets(node2)));
    
    ia_relation = IA_Network_Fetch_Relation(ia_network, i, j);
    if IA_Relation_Is_Greater(ia_relation)
      temp = scheduledTargets(node1);
      scheduledTargets(node1) = scheduledTargets(node2); %#ok<AGROW>
      scheduledTargets(node2) = temp; %#ok<AGROW>
    end
  end
end

if ~isempty(scheduledTargets)
  status = 1;
end
end %function